package io.mya.modules.sys.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import io.mya.common.annotation.SysLog;
import io.mya.common.utils.PageUtils;
import io.mya.common.utils.Query;
import io.mya.common.utils.R;
import io.mya.common.validator.ValidatorUtils;
import io.mya.modules.sys.entity.NoticeSettingEntity;
import io.mya.modules.sys.service.NoticeSettingService;

/**
 * 后台公告设置的controller
 * @author shican
 * @date 2017年9月15日
 * 1208296327@qq.com
 */
@RestController
@RequestMapping("/sys/noticeSetting")
public class NoticesettingController extends AbstractController{
	@Autowired
	private NoticeSettingService noticeSettingService;
	
	/**
	 * 查询所有公告
	 * 
	 * 
	 * 
	 */
	@RequestMapping("/findAll")
	public R findAll(@RequestParam Map<String, Object> params){
		params.put("userid", getUserId());
		//查询列表数据
		Query query = new Query(params);
		//PageHelper.startPage(1, 10);
		Page<NoticeSettingEntity> page = PageHelper.startPage(query.getPage(), query.getLimit());
		List<NoticeSettingEntity> list = noticeSettingService.query(query);
		//从page里面拿到表里面的总数 
		int total= new Long(page.getTotal()).intValue();  
		PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
		return R.ok().put("page", pageUtil);
	}
	@RequestMapping("/queryAll")
	public R queryAll(@RequestParam Map<String, Object> params){
//		params.put("userid", getUserId());
//		//查询列表数据
//		Query query = new Query(params);
//		//PageHelper.startPage(1, 10);
//		Page<NoticeSettingEntity> page = PageHelper.startPage(query.getPage(), query.getLimit());
//		List<NoticeSettingEntity> list = noticeSettingService.query(query);
//		//从page里面拿到表里面的总数 
//		int total= new Long(page.getTotal()).intValue();  
//		PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
//		return R.ok().put("page", pageUtil);
		List<NoticeSettingEntity> list =noticeSettingService.queryAll();
		return R.ok().put("list", list);
	}

	/**
	 * 根据ID查询公告信息 然后回显到修改页面
	 */
	
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") Integer id){
		NoticeSettingEntity noticeSetting = noticeSettingService.queryById(id);
		return R.ok().put("noticeSetting", noticeSetting);
	}
	
	/**
	 * 修改公告详情
	 */
	@SysLog("修改公告详情")
	@RequestMapping("/update")
	public R update(@RequestBody NoticeSettingEntity noticeSetting){
		ValidatorUtils.validateEntity(noticeSetting);
		noticeSetting.setCreTime(new Date());
		noticeSettingService.update(noticeSetting);
		return R.ok();
	}
	/**
	 * 增加公告详情
	 * @throws UnsupportedEncodingException 
	 */
	@SysLog("增加公告详情")
	@RequestMapping("/save")
	public R save(@RequestBody NoticeSettingEntity noticeSetting) throws UnsupportedEncodingException{
		ValidatorUtils.validateEntity(noticeSetting);
		String searchtext=java.net.URLDecoder.decode(noticeSetting.getTitle(),"UTF-8");/*需要处理异常*/
		Long userId = getUserId();
		noticeSetting.setUserid(userId);
		noticeSetting.setStatus(1);
		noticeSetting.setCreTime(new Date());
		noticeSettingService.save(noticeSetting);
		return R.ok();
	}
	/**
	 * 删除定时任务
	 */
	@SysLog("删除公告详情")
	@RequestMapping("/delete")
	public R delete(@RequestBody Integer[] ids){
		noticeSettingService.deleteBatch(ids);
		
		return R.ok();
	}
}
